package com.lemon.business;

import com.lemon.business.dao.InsertDataDao;
import com.lemon.entity.CpuDataVO;
import com.lemon.entity.DiskDataVO;
import com.lemon.entity.MachineVO;
import com.lemon.entity.MemoryDataVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * 入库job 消防job中的数据并入库
 * @author lenomSeven 1337857379@qq.com
 * @date 2020/10/6
 */
public class InsertDataService implements Runnable {
    private static final Logger LOG = LoggerFactory.getLogger(InsertDataService.class);

    private static boolean isRun = true;

    private final int MAX_DATA_COUNT = 200;

    // 30秒一次
    private final long MAX_DELAY = 30*1000;

    // cpu 上次入库时间
    private static long cpuLastInsertTime = System.currentTimeMillis();

    // disk 上次入库时间
    private static long diskLastInsertTime = System.currentTimeMillis();

    // machine 上次入库时间
    private static long machineLastInsertTime = System.currentTimeMillis();

    // memory 上次入库时间
    private static long memoryLastInsertTime = System.currentTimeMillis();

    @Override
    public void run() {
        LinkedBlockingDeque<Object> cache = DataCache.getCache();
        List<CpuDataVO> cpuDataVOS = new ArrayList<>();
        List<DiskDataVO> diskDataVOS = new ArrayList<>();
        List<MachineVO> machineVOS = new ArrayList<>();
        List<MemoryDataVO> memoryDataVOS = new ArrayList<>();
        while (isRun) {
            try {
                Object data = cache.pollLast(500, TimeUnit.MILLISECONDS);
                if (data != null) {
                    if (data instanceof CpuDataVO) {
                        cpuDataVOS.add((CpuDataVO) data);
                    } else if (data instanceof DiskDataVO) {
                        diskDataVOS.add((DiskDataVO) data);
                    } else if (data instanceof MachineVO) {
                        machineVOS.add((MachineVO) data);
                    } else if (data instanceof MemoryDataVO) {
                        memoryDataVOS.add((MemoryDataVO) data);
                    }
                }
                if (((System.currentTimeMillis() - cpuLastInsertTime > MAX_DELAY && cpuDataVOS.size() > 0) || cpuDataVOS.size() > MAX_DATA_COUNT)) {
                    InsertDataDao.insertCpu(cpuDataVOS);
                    cpuDataVOS.clear();
                    cpuLastInsertTime = System.currentTimeMillis();
                }
                if (((System.currentTimeMillis() - diskLastInsertTime > MAX_DELAY && diskDataVOS.size() > 0) || diskDataVOS.size() > MAX_DATA_COUNT)) {
                    InsertDataDao.insertDisk(diskDataVOS);
                    diskDataVOS.clear();
                    diskLastInsertTime = System.currentTimeMillis();
                }
                if ((System.currentTimeMillis() - machineLastInsertTime > MAX_DELAY && machineVOS.size() > 0) || machineVOS.size() > MAX_DATA_COUNT ) {
                    InsertDataDao.updateMachine(machineVOS);
                    machineVOS.clear();
                    machineLastInsertTime = System.currentTimeMillis();
                }
                if ((System.currentTimeMillis() - memoryLastInsertTime > MAX_DELAY && memoryDataVOS.size() > 0) || memoryDataVOS.size() > MAX_DATA_COUNT) {
                    InsertDataDao.insertMemory(memoryDataVOS);
                    memoryDataVOS.clear();
                    memoryLastInsertTime = System.currentTimeMillis();
                }
            } catch (Exception e) {
                LOG.error("InsertData error.", e);
            }
        }
    }

    public static void close(){
        isRun = false;
    }
}